package traverser;
/* The thread the KeywordConcurrentTraverser uses
 * Author: Mark 
 */
public class KeywordThread extends Thread{

	String[] links;  //received children links from the parent link
	int num;         // the thread number of this thread
	int threadNum;   //how many threads are running
	int[] linkPriority;
	KeyTracker tracker;
	String threadNextLink;
	int threadHighestPriority;
	String targetLink;

	public KeywordThread(String[] links,int num,int threadNum,KeyTracker tracker,String targetLink){
		this.links = links;
		this.num = num;
		this.threadNum = threadNum;
		this.tracker = tracker;
		linkPriority = new int[links.length/threadNum+1];
		this.targetLink = targetLink;
	}

	public void run(){
		//distribute the assignments for the thread according to the threadNum of this thread
		if(num<threadNum-1){
			int n=0;
			for(int i=num*(links.length/threadNum+1);i<(num+1)*(links.length/threadNum+1);i++){
				linkPriority[n]=findPriority(links[i]);	
				//	System.out.println("Thread "+num+" found "+links[i]+" with priority of "+linkPriority[n]);
				n++;
			}
		}
		else{
			int n=0;
			for(int i=num*(links.length/threadNum+1);i<links.length;i++){
				linkPriority[n]=findPriority(links[i]);	
				//	System.out.println("Thread "+num+" found "+links[i]+" with priority of "+linkPriority[n]);
				n++;
			}
		}
		//find out the link with the highest priority among the links that have been searched through in this thread
		threadHighestPriority=0;
		for(int i=0;i<linkPriority.length;i++){
			if(threadHighestPriority<linkPriority[i]){
				threadNextLink=links[i+num*(links.length/threadNum+1)];
				threadHighestPriority=linkPriority[i];
			}
		}

	}

	public int findPriority(String link){
		CacheLinkProcessor lp=new CacheLinkProcessor(link);
		int priority=0;
		String[] subLinks=new String[lp.count()];
		lp.removeDuplicates();
		subLinks=lp.getLinks();
		String[][] tempKey=new String[lp.count()][];
		//firstly try to find if the target link is inside the links of this thread
		for(int j=0;j<subLinks.length;j++){ 
			if(subLinks[j].equals(targetLink)&&!link.equals("http://en.wikipedia.org/wiki/Main_Page")){
				System.out.println("Visit the link "+link+" lead to the target "+(System.currentTimeMillis()-KeywordConcurrentTraverser.startTime));
				KeywordConcurrentTraverser.ifFoundTargetLink=true;
				stop();
			}
		}
		//if not, count the priority of each link
		for(int j=0;j<subLinks.length;j++){     //the keyword of next layer links
			if(KeywordConcurrentTraverser.ifFoundTargetLink==true) stop();
			String tempString=subLinks[j];
			tempString=tempString.substring(29,tempString.length());

			tempString=tempString.replace("of_","");
			tempString=tempString.replace("the_","");
			tempString=tempString.replace("and_","");
			tempString=tempString.replace("with_","");
			tempString=tempString.replace("or_","");
			tempString=tempString.replace("(","");
			tempString=tempString.replace(")","");
			tempString=tempString.replace("%27","'");
			tempString=tempString.toLowerCase();
			tempKey[j]=tempString.split("_");			
		}
		for(int j=0;j<tempKey.length;j++){                     
			for(int k=0;k<tempKey[j].length;k++){
				priority+=tracker.priorityOf(tempKey[j][k]);
			}
		}

		return priority;		
	}
	//return the link with the highest priority
	public String getNextlink(){
		return threadNextLink;
	}
	//return the link priority of the threadNextLink 
	public int getHighestPriority(){
		return threadHighestPriority;
	}
}
